// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <tests/substrait/json_to_proto_converter.h>

#include <pollux/common/base/fs.h>
#include <pollux/testing/dwio/data_files.h>
#include <pollux/testing/exec/util/operator_test_base.h>
#include <pollux/plan/plan_builder.h>
#include <pollux/testing/vector/vector_test_base.h>

#include <pollux/substrait/substrait_to_pollux_plan.h>

using namespace kumo::pollux;
using namespace kumo::pollux::test;
using namespace kumo::pollux::exec;
using namespace kumo::pollux::exec::test;
using namespace kumo::pollux::substrait;

class Substrait2PolluxValuesNodeConversionTest : public OperatorTestBase {
protected:
    std::shared_ptr<SubstraitPolluxPlanConverter> planConverter_ =
            std::make_shared<SubstraitPolluxPlanConverter>(pool_.get());
};

// SELECT * FROM tmp
TEST_F(Substrait2PolluxValuesNodeConversionTest, valuesNode) {
    auto planPath = getDataFilePath(
        "pollux/substrait/tests", "data/substrait_virtualTable.json");

    ::substrait::Plan substraitPlan;
    JsonToProtoConverter::readFromFile(planPath, substraitPlan);

    auto polluxPlan = planConverter_->toPolluxPlan(substraitPlan);

    RowVectorPtr expectedData = make_row_vector(
        {
            make_flat_vector<int64_t>(
                {2499109626526694126, 2342493223442167775, 4077358421272316858}),
            make_flat_vector<int32_t>({581869302, -708632711, -133711905}),
            make_flat_vector<double>(
                {0.90579193414549275, 0.96886777112423139, 0.63235925003444637}),
            make_flat_vector<bool>({true, false, false}),
            make_flat_vector<int32_t>(3, nullptr, null_every(1))

        });

    createDuckDbTable({expectedData});
    assertQuery(polluxPlan, "SELECT * FROM tmp");
}
